package org.jsbd.boss.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.Resource;

import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.dao.IMiguVideoRetentAddStatisticsDao;
import org.jsbd.boss.dao.IMiguVideoSearchDAO;
import org.jsbd.boss.domian.MiguVideoRetentAddStatistics;
import org.jsbd.boss.domian.MiguVideoRetentDoNumStatistics;
import org.jsbd.boss.domian.MiguVideoSearchVo;
import org.jsbd.boss.service.IMiguVideoSearchService;
import org.springframework.stereotype.Service;

@Service("miguVideoSearchService")
public class MiguVideoSearchServiceImpl implements IMiguVideoSearchService {

	@Resource
	private IMiguVideoSearchDAO miguVideoSearchDao;

	@Resource
	private IMiguVideoRetentAddStatisticsDao miguVideoRetentAddStatisticsDao;

	@Override
	public Map<String, Object> getNewTaskExecListPage(DataGridModel page, MiguVideoSearchVo miguVideoSearchVo) {
		return miguVideoSearchDao.selectNewTaskExecListPage(page, miguVideoSearchVo);
	}

	@Override
	public Map<String, Object> getRetentTaskExecList(MiguVideoSearchVo miguVideoSearchVo) {
		// 咪咕留存执行统计
		List<MiguVideoRetentDoNumStatistics> list1 = miguVideoSearchDao.selectRetentTaskExecList(miguVideoSearchVo);
		// 咪咕留存补量执行统计
		List<MiguVideoRetentAddStatistics> list2 = miguVideoRetentAddStatisticsDao.getMiguDayList(miguVideoSearchVo);

		TreeMap<String, MiguVideoRetentDoNumStatistics> map = new TreeMap<>();
		for (MiguVideoRetentDoNumStatistics retent : list1) {
			retent.setBlNum(Integer.valueOf(0));
			map.put(retent.getKey(), retent);
		}

		for (MiguVideoRetentAddStatistics add : list2) {
			MiguVideoRetentDoNumStatistics retent = map.get(add.getKey());
			if (retent == null) {
				map.put(add.getKey(), add.getMiguVideoRetentDoNumStatistics());
			} else {
				retent.setBlNum(add.getDoNum());
			}
		}

		List<MiguVideoRetentDoNumStatistics> list3 = new ArrayList<>(map.size());
		for (Map.Entry<String, MiguVideoRetentDoNumStatistics> entry : map.entrySet()) {
			list3.add(entry.getValue());
		}

		Map<String, Object> results = new HashMap<String, Object>();
		results.put("rows", list3);
		return results;
	}

}
